package com.equipment.fkdcn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.*;
import com.equipment.fkdcn.service.DepartmentService;
import com.equipment.fkdcn.service.EReturnLendService;
import com.equipment.fkdcn.service.ETypeService;
import com.equipment.fkdcn.service.EparameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/*
*
* @借还管理
* */
@Controller
@RequestMapping("/lr")
public class EReturnLendController {

    @Autowired
    @Qualifier("eReturnLendService")
    private EReturnLendService lendService;

    @Autowired
    @Qualifier("departmentService")
    private DepartmentService departmentService;


    @Autowired
    @Qualifier("eparameterService")
    private EparameterService eparameterService;

    @Autowired
    @Qualifier("eTypeService")
    private ETypeService eTypeService;


    private  List<Params> getParams(List<EReturnLend> list){
        List<Params> tmpList=new ArrayList<Params>();
        for (EReturnLend er:list) {
            Department department = er.getDepartment();
            int eId = er.geteId();
            EParameter ep = er.getEpId();
            EType etype = ep.getEtype();
            String eName = ep.geteName();
            String dpt = department.getLendDepar();
            String brand = ep.getBrand();
            String model = ep.getModel();
            String mf = ep.getManufacturer();
            int num = ep.getNum();
            String dt = ep.getDeliveryTime();
            String pt = ep.getPurchaseTime();
            String ggent = er.getGgent();
            String borrower = er.getBorrower();
            int lendNum = er.getLendNum();
            String type = etype.getType();
            String state = er.getState();
            String lendTime = er.getLendTime();
            /*String reTime = er.getReTime();*/
            Params params = new Params(eId,eName,type,brand,model,mf,num,dt,pt,ggent,borrower,lendNum,dpt,lendTime,state);
            tmpList.add(params);
        }
        return  tmpList;
    }


    /*查询所有*/
    @GetMapping("/lrparams")
    @ResponseBody
    public String params(@RequestParam("param") String data){
        List<EReturnLend> eReturnLends = lendService.allQuery("已借出");
        List<Params>   list= getParams(eReturnLends);
        List<Params> list2=new ArrayList<Params>();
        if(data!=null&&data!=""){
            for (int i=0;i<list.size();i++){
                Params params = list.get(i);
                Field[] fields = params.getClass().getDeclaredFields();
                for (int j=0;j<fields.length;j++) {
                    Field f = fields[j];
                    f.setAccessible(true);
                    try {
                        System.out.println(data);
                        if (f.get(params).toString().indexOf(data)>=0){
                            list2.add(params);
                            break;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }

                }
            }

            list=list2;
        }
        String params= JSON.toJSONString(list);
        return params;
    }




    /*查询归还列表*/
    @GetMapping("/reList")
    @ResponseBody
    public String reList(@RequestParam("param") String data){
        List<EReturnLend> eReturnLends = lendService.allQuery("已归还");
        List<Params>   list= getParams(eReturnLends);
        List<Params> list2=new ArrayList<Params>();
            for (int i=0;i<list.size();i++){
                Params params = list.get(i);
                Field[] fields = params.getClass().getDeclaredFields();
                for (int j=0;j<fields.length;j++) {
                    Field f = fields[j];
                    f.setAccessible(true);
                    try {
                        if (f.get(params).toString().indexOf(data)>=0){
                            list2.add(params);
                            break;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }

                }
            }
            list=list2;

        String params= JSON.toJSONString(list);
        return params;
    }



    /*查看借出和归还记录*/
    @GetMapping("/state")
    @ResponseBody
    public String newparams(@RequestParam("param") String name){
        List<EReturnLend> eReturnLends = lendService.allQuery(name);
        List<Params> list=getParams(eReturnLends);
        String params= JSON.toJSONString(list);
        return  params;
    }



    //登记归还
    @PostMapping("/addRet")
    @ResponseBody
    public String reAdd(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        EReturnLend eReturnLend = jsonObject.toJavaObject(EReturnLend.class);
        boolean b = lendService.lendAdd(eReturnLend);
        return  "{\"state\":"+b+"}";
    }


    //删除功能
    @GetMapping("del")
    @ResponseBody
    public String delLRParam(@RequestParam("param")String  data){
        String newData = data.substring(1, data.length() - 1);
        String[] list = newData.split(",");
        //转换为int数组
        boolean flag=true;
       for (int i=0;i<list.length;i++){
           boolean b = lendService.dellenReturn(Integer.parseInt(list[i]));
           if (!b){
               flag=false;
               break;
           }
       }
        return  "{\"state\":"+flag+"}";
    }



    //登记借出
    @PostMapping("/addLend")
    @ResponseBody
    public  String addLend(@RequestBody String data){
        System.out.println(data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        EReturnLend eReturnLend =JSON.toJavaObject(jsonObject, EReturnLend.class);
        boolean b = lendService.lendAdd(eReturnLend);
        return  "{\"state\":"+b+"}";
    }


    //查询设备
    @PostMapping("/queryEp")
    @ResponseBody
    public String queryName(@RequestBody  String data){
            System.out.println(data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String eName = jsonObject.getString("eName");
        String typeId = jsonObject.getString("typeId");

        List<EParameter> ep = eparameterService.queryName(eName,typeId);
        int ied=0;
        if (ep.size()>0) ied=ep.get(0).getEid();

        boolean f=ep.size()>0?true:false;
        return  "{\"state\":"+f+",\"eId\":"+ied+"}";
    }








    //归还
    @GetMapping("/addRetEp")
    @ResponseBody
    public  String addRetEp(@RequestParam("param")String data){
        JSONObject jsonObject = JSON.parseObject(data);

        String ename = jsonObject.getString("ename");
        String etype = jsonObject.getString("etype");
        String name = jsonObject.getString("name");
        String model = jsonObject.getString("model");
        String endTime = jsonObject.getString("endTime");
        String dep = jsonObject.getString("dep");
        List<EReturnLend> eReturnLends = lendService.retuEp(name,dep);
        boolean flag=false;
        if (eReturnLends.size()>0){
            for (EReturnLend eret:eReturnLends) {
                String epName=eret.getEpId().geteName();
                String typeID=""+eret.getEpId().getEtype().getId();
                String mode=eret.getEpId().getModel();
                System.out.println(epName+"==="+ename +"\t"+typeID+"===="+etype+"\t"+mode+"====="+model);
                if (epName.equals(ename)&&typeID.equals(etype)&&mode.equals(model)){
                    flag=true;
                    boolean b = lendService.upLendReturn(endTime, "已归还",eret.geteId());
                    break;
                }
            }
        }

//        for (EReturnLend e : eReturnLends) {
//            String eName = e.getEpId().geteName();
//            int id = e.getEpId().getEtype().getId();
//            String md2 = e.getEpId().getModel();
//            String borrower = e.getBorrower();
//            int dpId = e.getDepartment().getId();
//            String reTime = e.getReTime();
//            if (!eName.equals(eName1)){
//
//               return "{\"numState\":1}";
//
//            }else if (id!= Integer.parseInt(etype)){
//                return "{\"numState\":2}";
//
//            }else if (!md2.equals(model)){
//
//                return "{\"numState\":3}";
//
//            }else if (!name.equals(borrower)){
//
//                return "{\"numState\":4}";
//
//
//            } else{
//                //添加归还日期
//
//                return  "{\"state\":"+b+"}";
//            }
//        }

        return  "{\"state\":"+flag+"}";
    }


}
